<?php

namespace frontend\modules\yii\models;

use Yii;

/**
 * This is the model class for table "frame_dir".
 *
 * @property int $id
 * @property string $dname The name of article dir.
 * @property int $pid The parent dir of this dir.
 * @property int $created_at The create time of this record;
 * @property int $updated_at The last update time of this record;
 */
class FrameDir extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'frame_dir';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'created_at', 'updated_at'], 'integer'],
            [['dname'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'dname' => 'Dname',
            'pid' => 'Pid',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public static function getNodes($pid = 0){
        set_time_limit(2);
        return array_map(function($node){
            if(self::find()->where(["pid" => $node->id])->exists()) {
                return ["id" => $node->id, "name" => $node->dname, "open" => true, "children" => self::getNodes($node->id)];
            }
            return ["id" => $node->id, "name" => $node->dname, "open" => true];
        }, self::find()->select("id,dname")->where(["pid" => $pid])->all());

    }
}
